package demo;

import java.util.Stack;

/**
 *  有效的括号
 */
class Solution17 {
	public boolean isValid(String s) {
		int length = s.length();
		for (int i = 0; i < length; i++) {
			s = s.replace("()", "").replace("[]", "").replace("{}", "");
		}
		return s.length() == 0;
	}

	public boolean isValidByStack(String s) {
		Stack<Character> stack = new Stack<>();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c == '(') {
				stack.push(')');
			} else if (c == '[') {
				stack.push(']');
			} else if (c == '{') {
				stack.push('}');
			} else if (stack.isEmpty() || stack.pop() != c) {
				return false;
			}
		}
		return stack.isEmpty();
	}
}

